/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shapematch;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import net.jasonbarnwell.java.Basics;

/**
 *
 * @author Jason  Barnwell
 * 13/0719/2191
 */
public class NewGameDialog implements ActionListener {
    private int difficulty;
    private JLabel difficultyImage = new JLabel();
    private JButton easyDifficulty_btn, mediumDifficulty_btn, hardDifficulty_btn, ok_btn, cancel_btn;
    private JPanel panel = new JPanel();
    private JOptionPane pane = new JOptionPane();
    private JDialog dialog = new JDialog();
    
    public NewGameDialog() {
        difficulty = -1; 
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        easyDifficulty_btn = new JButton("<html>Easy<br>12 Tiles</html>");
        mediumDifficulty_btn = new JButton("<html>Medium<br>16 Tiles</html>");
        hardDifficulty_btn = new JButton("<html>Hard<br>36 Tiles</html>");
        ok_btn = new JButton("Start Game");
        cancel_btn = new JButton("Cancel");
        
        easyDifficulty_btn.addActionListener(this);
        mediumDifficulty_btn.addActionListener(this);
        hardDifficulty_btn.addActionListener(this);
        ok_btn.addActionListener(this);
        cancel_btn.addActionListener(this);
        
        setDifficultyImage("images/halo.png");
        panel.add(difficultyImage);
        panel.add(easyDifficulty_btn);
        panel.add(mediumDifficulty_btn);
        panel.add(hardDifficulty_btn);
        
        //return difficulty;  //no need
    }
    
    public int requestDifficulty(){
        pane = createPane();
        dialog = pane.createDialog(null, "New Game");
        dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        dialog.setVisible(true);
        
        System.out.println();
        
        return difficulty;
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == easyDifficulty_btn){
            difficulty = GameEngine.EASY;
            setDifficultyImage("images/easy.png");
        }
        else if(e.getSource() == mediumDifficulty_btn){
            difficulty = GameEngine.MEDIUM;
            setDifficultyImage("images/medium.png");
        }        
        else if(e.getSource() == hardDifficulty_btn){
            difficulty = GameEngine.HARD;
            setDifficultyImage("images/hard.png");
        }
        else if(e.getSource() == ok_btn){
            if(difficulty != -1){
                dialog.dispose();
            }
            else{
                JOptionPane.showMessageDialog(null, "You must select the game's difficulty in order to \nstart a new game", "Select Difficulty", JOptionPane.ERROR_MESSAGE);
            }
        }
        else if(e.getSource() == cancel_btn){
            difficulty = -1;
            dialog.dispose();
        }
        
    }
    
    private JOptionPane createPane(){
        return new JOptionPane( panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, new Object[]{ok_btn, cancel_btn});
    }
    
    private void setDifficultyImage(String path){
        difficultyImage.setIcon(Basics.getScaledImageIcon(path, 100,100));   //adding some badass images from Halo difficulty selct
    }
    
}
